import { themeList } from '@/api/theme'
import storage from '@/utils/storage'

// 默认配置
const theme = {
  state: {
    info: {
      mobileLoginBackgroundName: '油运宝-管控',
      logiMobileUrl: {
		  url:''
	  },
      logoUrl: {
		  url:'../static/logo.png',
		},
	  logiLogoUrl:{
		  url:'../static/logo.png',
		}
    }
  },
  
  mutations: {
    SET_Theme: (state, info) => {
      state.info = {
        mobileLoginBackgroundName: info?.mobileLoginBackgroundName || state.info.mobileLoginBackgroundName,
        logiMobileUrl: info?.logiMobileUrl || state.info.logiMobileUrl,
        logoUrl: info?.logoUrl || state.info.logoUrl,
		logiLogoUrl:info?.logiLogoUrl || state.info.logiLogoUrl,
      }

      // 拿到最新基础消息并保存到本地
	  uni.setStorageSync('info', JSON.stringify(state.info))
    }
  },

  actions: {
    getTheme({ commit }) {
		const info = uni.getStorageSync('info')
		themeList().then(res => {
			let item=res.rows.find(row => row.deptId==='100')
			let item2=res.rows.find(row => row.deptId==uni.getStorageSync('storage_data')?uni.getStorageSync('storage_data').vuex_userInfo.dept.deptId:'')
			if(info)  //如果存在
			{
				let pare_info=JSON.parse(info);
				if(item.mobileLoginBackgroundName!==pare_info.mobileLoginBackgroundName||item.logoUrl.url!==pare_info.logoUrl.url||item.logiMobileUrl.url!==pare_info.logiMobileUrl.url||item.logiLogoUrl.url!==pare_info.logiLogoUrl.url)
				{
					let form={
						logoUrl:item.logoUrl?item.logoUrl:{url:'../static/logo.png'},  //运营平台logo
						mobileLoginBackgroundName:item.mobileLoginBackgroundName?item.mobileLoginBackgroundName:'油运宝-管控',  //移动登录页面背景名称
						logiMobileUrl:item.logiMobileUrl?item.logiMobileUrl:{url:''},    //移动登录页面背景图
						logiLogoUrl:item2?item2.logiLogoUrl:{url:'../static/logo.png'},    //物流服务商logo图
					}
					commit('SET_Theme', form)
				}
			}
			else
			{
				let form={
					logoUrl:item.logoUrl?item.logoUrl:{url:'../static/logo.png'},  //运营平台logo
					mobileLoginBackgroundName:item.mobileLoginBackgroundName?item.mobileLoginBackgroundName:'油运宝-管控',  //移动登录页面背景名称
					logiMobileUrl:item.logiMobileUrl?item.logiMobileUrl:{url:''},    //移动登录页面背景图
					logiLogoUrl:item2?item2.logiLogoUrl:{url:'../static/logo.png'},    //物流服务商logo图
				}
				commit('SET_Theme', form)
			}
		})
    }
  }
}

export default theme
